package com.ys.service.base.impl;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.ys.dto.AddDto;
import com.ys.entity.base.TbUserTaxManage;
import com.ys.entity.base.TbUserTaxManageItem;
import com.ys.entity.base.TbUserTaxRate;
import com.ys.entity.sys.YsCol;
import com.ys.mapper.base.*;
import com.ys.service.base.TbUserTaxManageService;
import com.ys.util.CheckUtil;
import com.ys.vo.R;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
import java.util.function.Predicate;


/**
 * 员工个税管理
 */
@Service("tbUserTaxManageService")
public class TbUserTaxManageServiceImpl implements TbUserTaxManageService {
	private static final Logger logger = LoggerFactory.getLogger(TbUserTaxManageServiceImpl.class);

	@Autowired
	private TbUserTaxManageMapper tbUserTaxManageMapper;

	@Autowired
	private TbUserTaxRateMapper tbUserTaxRateMapper;

	@Autowired
	private TbUserTaxManageItemMapper tbUserTaxManageItemMapper;

	@Autowired
	private CheckUtil checkUtil;

	/**
	 * 保存
	 */
	@Override
	public R save(Map<String, Object> params, Long menuId, Long menuIdByTaxRate, Long menuIdByItem) throws Exception {
		logger.info("员工个税管理保存传参："+params);

		//参数验证
		String errorMsg = checkParams(params,menuId);
		if(errorMsg!=null && !"".equals(errorMsg)){
			return R.error(errorMsg);
		}
		TbUserTaxManage tbUserTaxManage = JSON.parseObject(JSON.toJSONString(params), TbUserTaxManage.class);
		tbUserTaxManage.setStatus(1);

		JSONObject jsonObject = JSONObject.parseObject(params.get("subData").toString());//获取列表信息
		String itemListStr = jsonObject.getString("itemList");//子表信息
		String taxRateListStr = jsonObject.getString("taxRateList");//员工个税税率表

		//主表信息保存
		tbUserTaxManageMapper.update(tbUserTaxManage);

		//保存，员工个税税率表
		if(taxRateListStr!=null && !"".equals(taxRateListStr)){
			JSONArray arrayInfo = JSON.parseArray(taxRateListStr);
			//参数验证
			String errorMsgBySub = checkParamsByTaxRate(arrayInfo,menuIdByTaxRate);
			if(errorMsgBySub!=null && !"".equals(errorMsgBySub)){
				return R.error(errorMsgBySub);
			}
			if(arrayInfo!=null && arrayInfo.size()>0){
				logger.info("员工个税税率表信息："+arrayInfo);
				for(int i=0;i<arrayInfo.size();i++){
					TbUserTaxRate tbUserTaxRate = JSON.parseObject(JSON.toJSONString(arrayInfo.get(i)), TbUserTaxRate.class);
					tbUserTaxRate.setStatus(1);
					tbUserTaxRate.setTaxManageId(tbUserTaxManage.getId());
					if(tbUserTaxRate.getId()!=null){
						tbUserTaxRateMapper.update(tbUserTaxRate);
					}else {
						tbUserTaxRateMapper.insert(tbUserTaxRate);
					}
				}
			}
		}

		//保存，员工个税管理子表
		if(itemListStr!=null && !"".equals(itemListStr)){
			JSONArray arrayInfo = JSON.parseArray(itemListStr);
			//参数验证
			String errorMsgBySub = checkParamsByItem(arrayInfo,menuIdByItem);
			if(errorMsgBySub!=null && !"".equals(errorMsgBySub)){
				return R.error(errorMsgBySub);
			}
			if(arrayInfo!=null && arrayInfo.size()>0){
				logger.info("员工个税管理子表信息："+arrayInfo);
				for(int i=0;i<arrayInfo.size();i++){
					TbUserTaxManageItem tbUserTaxManageItem = JSON.parseObject(JSON.toJSONString(arrayInfo.get(i)), TbUserTaxManageItem.class);
					tbUserTaxManageItem.setStatus(1);
					tbUserTaxManageItem.setTaxManageId(tbUserTaxManage.getId());
					if(tbUserTaxManageItem.getId()!=null){
						tbUserTaxManageItemMapper.update(tbUserTaxManageItem);
					}else {
						tbUserTaxManageItemMapper.insert(tbUserTaxManageItem);
					}
				}
			}
		}
		return R.ok(AddDto.gotoEdit("/web/base/tbUserTaxManage/toEdit?id=" + tbUserTaxManage.getId()));
		//return R.ok();
	}



	/**
	 * 参数验证
	 */
	public String checkParams(Map<String, Object> params, Long menuId) throws Exception {
		//获取pc字段数据
		List<YsCol> listField = new YsCol()
				.where("A.MenuId = "+menuId)
				.setOrderby("A.SortNo ASC")
				.queryList();
		//验证必填字段的非空；其他字段的长度限制、类型
		String backMsg = checkUtil.checkTableColLengthAndDataType("TB_UserTaxManage",listField,params,ysColItem->ysColItem.getFormEditRequired()!=null && ysColItem.getFormEditRequired() == 1);
		return backMsg;
	}


	/**
	 * 员工个税税率表信息 参数验证
	 */
	public String checkParamsByTaxRate(JSONArray subData, Long menuId) throws Exception {
		String backMsg = "";
		if(subData!=null && subData.size()>0){
			//获取pc字段数据
			List<YsCol> listField = new YsCol()
					.where("A.MenuId = "+menuId)
					.setOrderby("A.SortNo ASC")
					.queryList();
			for (int i = 0; i < subData.size(); i++) {
				JSONObject msg = subData.getJSONObject(i);
				if(msg==null){
					backMsg = "子表信息第"+(i+1)+"行的参数为空！";
					continue;
				}
				//验证必填字段的非空；其他字段的长度限制、类型
				backMsg = checkUtil.checkTableColLengthAndDataType("TB_UserTaxRate",listField,msg,ysColItem->ysColItem.getFormEditRequired()!=null && ysColItem.getFormEditRequired() == 1);
			}
		}
		return backMsg;
	}


	/**
	 * 员工个税管理子表信息 参数验证
	 */
	public String checkParamsByItem(JSONArray subData, Long menuId) throws Exception {
		String backMsg = "";
		if(subData!=null && subData.size()>0){
			//获取pc字段数据
			List<YsCol> listField = new YsCol()
					.where("A.MenuId = "+menuId)
					.setOrderby("A.SortNo ASC")
					.queryList();
			for (int i = 0; i < subData.size(); i++) {
				JSONObject msg = subData.getJSONObject(i);
				if(msg==null){
					backMsg = "子表信息第"+(i+1)+"行的参数为空！";
					continue;
				}
				//验证必填字段的非空；其他字段的长度限制、类型
				backMsg = checkUtil.checkTableColLengthAndDataType("TB_UserTaxManageItem",listField,msg,ysColItem->ysColItem.getFormEditRequired()!=null && ysColItem.getFormEditRequired() == 1);
			}
		}
		return backMsg;
	}



	/**
	 * 删除员工个税税率表
	 */
	@Override
	public void toDelTaxRate(Long id) throws Exception {
		tbUserTaxRateMapper.deleteTrue(id);
	}


	/**
	 * 删除员工个税管理子表
	 */
	@Override
	public void toDelItem(Long id) throws Exception {
		tbUserTaxManageItemMapper.deleteTrue(id);
	}



}
